<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>表单绑定功能测试</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 1200px;
            margin: 0 auto;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .container {
            background: white;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0,0,0,0.1);
        }
        h1 {
            color: #333;
            text-align: center;
            margin-bottom: 30px;
        }
        .section {
            margin-bottom: 30px;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 6px;
            background: #fafafa;
        }
        .section h2 {
            color: #555;
            margin-top: 0;
            border-bottom: 2px solid #007bff;
            padding-bottom: 10px;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: block;
            margin-bottom: 5px;
            font-weight: bold;
            color: #333;
        }
        input, select, textarea {
            width: 100%;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
            font-size: 14px;
        }
        button {
            background: #007bff;
            color: white;
            padding: 12px 24px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            font-size: 16px;
            margin-right: 10px;
        }
        button:hover {
            background: #0056b3;
        }
        .result {
            margin-top: 20px;
            padding: 15px;
            border-radius: 4px;
            background: #e9ecef;
            border-left: 4px solid #007bff;
        }
        .error {
            background: #f8d7da;
            border-left-color: #dc3545;
            color: #721c24;
        }
        .success {
            background: #d4edda;
            border-left-color: #28a745;
            color: #155724;
        }
        .api-url {
            font-family: monospace;
            background: #f8f9fa;
            padding: 5px;
            border-radius: 3px;
            font-size: 12px;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>🔗 表单绑定功能测试</h1>
        
        <!-- 1. 创建任务并绑定表单 -->
        <div class="section">
            <h2>1. 创建任务并绑定表单</h2>
            <div class="form-group">
                <label>任务标题:</label>
                <input type="text" id="taskTitle" value="测试表单绑定任务" placeholder="请输入任务标题">
            </div>
            <div class="form-group">
                <label>表单ID:</label>
                <input type="text" id="formId" value="form_001" placeholder="请输入表单ID">
            </div>
            <div class="form-group">
                <label>表单数据ID:</label>
                <input type="text" id="formDataId" value="formdata_001" placeholder="请输入表单数据ID">
            </div>
            <div class="form-group">
                <label>执行人ID:</label>
                <input type="text" id="assigneeId" value="user_001" placeholder="请输入执行人ID">
            </div>
            <button onclick="createTask()">创建任务</button>
            <div class="api-url">API: POST /api/v1/tasks</div>
            <div id="createResult" class="result" style="display:none;"></div>
        </div>
        
        <!-- 2. 查询任务详情 -->
        <div class="section">
            <h2>2. 查询任务详情（验证表单信息加载）</h2>
            <div class="form-group">
                <label>任务ID:</label>
                <input type="text" id="queryTaskId" placeholder="请输入任务ID">
            </div>
            <button onclick="getTaskDetail()">查询任务详情</button>
            <div class="api-url">API: GET /api/v1/tasks/{id}</div>
            <div id="queryResult" class="result" style="display:none;"></div>
        </div>
        
        <!-- 3. 根据表单ID查询任务 -->
        <div class="section">
            <h2>3. 根据表单ID查询任务列表</h2>
            <div class="form-group">
                <label>表单ID:</label>
                <input type="text" id="searchFormId" value="form_001" placeholder="请输入表单ID">
            </div>
            <button onclick="getTasksByFormId()">查询任务列表</button>
            <div class="api-url">API: GET /api/v1/tasks/form/{formId}</div>
            <div id="formSearchResult" class="result" style="display:none;"></div>
        </div>
        
        <!-- 4. 根据表单数据ID查询任务 -->
        <div class="section">
            <h2>4. 根据表单数据ID查询任务</h2>
            <div class="form-group">
                <label>表单数据ID:</label>
                <input type="text" id="searchFormDataId" value="formdata_001" placeholder="请输入表单数据ID">
            </div>
            <button onclick="getTaskByFormDataId()">查询任务</button>
            <div class="api-url">API: GET /api/v1/tasks/form-data/{formDataId}</div>
            <div id="formDataSearchResult" class="result" style="display:none;"></div>
        </div>
        
        <!-- 5. 任务列表查询（支持表单ID过滤） -->
        <div class="section">
            <h2>5. 任务列表查询（支持表单ID过滤）</h2>
            <div class="form-group">
                <label>表单ID过滤:</label>
                <input type="text" id="listFormId" value="form_001" placeholder="可选：按表单ID过滤">
            </div>
            <button onclick="getTaskList()">查询任务列表</button>
            <div class="api-url">API: GET /api/v1/tasks/list?formId={formId}</div>
            <div id="listResult" class="result" style="display:none;"></div>
        </div>
    </div>
    
    <script>
        const API_BASE = 'http://localhost:8080';
        
        // 创建任务
        async function createTask() {
            const data = {
                title: document.getElementById('taskTitle').value,
                description: '这是一个测试表单绑定功能的任务',
                type: 'FORM',
                category: 'TEST',
                priority: 3,
                assigneeId: document.getElementById('assigneeId').value,
                assigneeName: '测试用户',
                formId: document.getElementById('formId').value,
                formDataId: document.getElementById('formDataId').value,
                businessType: 'FORM_BINDING_TEST',
                businessKey: 'test_' + Date.now()
            };
            
            try {
                const response = await fetch(`${API_BASE}/api/v1/tasks`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                        'X-User-Id': 'test_user',
                        'X-User-Name': '测试用户'
                    },
                    body: JSON.stringify(data)
                });
                
                const result = await response.json();
                showResult('createResult', result, response.ok);
                
                if (response.ok && result.data && result.data.id) {
                    document.getElementById('queryTaskId').value = result.data.id;
                }
            } catch (error) {
                showResult('createResult', { message: '请求失败: ' + error.message }, false);
            }
        }
        
        // 查询任务详情
        async function getTaskDetail() {
            const taskId = document.getElementById('queryTaskId').value;
            if (!taskId) {
                alert('请输入任务ID');
                return;
            }
            
            try {
                const response = await fetch(`${API_BASE}/api/v1/tasks/${taskId}`);
                const result = await response.json();
                showResult('queryResult', result, response.ok);
            } catch (error) {
                showResult('queryResult', { message: '请求失败: ' + error.message }, false);
            }
        }
        
        // 根据表单ID查询任务
        async function getTasksByFormId() {
            const formId = document.getElementById('searchFormId').value;
            if (!formId) {
                alert('请输入表单ID');
                return;
            }
            
            try {
                const response = await fetch(`${API_BASE}/api/v1/tasks/form/${formId}`);
                const result = await response.json();
                showResult('formSearchResult', result, response.ok);
            } catch (error) {
                showResult('formSearchResult', { message: '请求失败: ' + error.message }, false);
            }
        }
        
        // 根据表单数据ID查询任务
        async function getTaskByFormDataId() {
            const formDataId = document.getElementById('searchFormDataId').value;
            if (!formDataId) {
                alert('请输入表单数据ID');
                return;
            }
            
            try {
                const response = await fetch(`${API_BASE}/api/v1/tasks/form-data/${formDataId}`);
                const result = await response.json();
                showResult('formDataSearchResult', result, response.ok);
            } catch (error) {
                showResult('formDataSearchResult', { message: '请求失败: ' + error.message }, false);
            }
        }
        
        // 查询任务列表
        async function getTaskList() {
            const formId = document.getElementById('listFormId').value;
            let url = `${API_BASE}/api/v1/tasks/list?pageNum=1&pageSize=10`;
            if (formId) {
                url += `&formId=${formId}`;
            }
            
            try {
                const response = await fetch(url);
                const result = await response.json();
                showResult('listResult', result, response.ok);
            } catch (error) {
                showResult('listResult', { message: '请求失败: ' + error.message }, false);
            }
        }
        
        // 显示结果
        function showResult(elementId, result, isSuccess) {
            const element = document.getElementById(elementId);
            element.style.display = 'block';
            element.className = `result ${isSuccess ? 'success' : 'error'}`;
            element.innerHTML = `
                <h4>${isSuccess ? '✅ 成功' : '❌ 失败'}</h4>
                <pre>${JSON.stringify(result, null, 2)}</pre>
            `;
        }
        
        // 页面加载完成后的提示
        window.onload = function() {
            console.log('表单绑定功能测试页面已加载');
            console.log('请确保后端服务已启动在 http://localhost:8080');
        };
    </script>
</body>
</html>